﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nature.Toolkit
{
    public class DynamicCodeHelper
    {
        public static string Sign(double value)
        {
            return value < 0.0 ? "-" : "+";
        }
        public static string Pow(string value, double exponent)
        {
            int p = (int)Math.Floor(exponent);
            if (exponent != p || Math.Abs(p) > 8) { return string.Format("Math.Pow({0}, {1})", value, exponent); }
            StringBuilder sb = new StringBuilder();
            if (p > 0)
            {
                int counter = 0;
                while ((p--) > 0)
                {
                    sb.AppendFormat("{0}({1})", counter++ == 0 ? string.Empty : " * ", value);
                }
            }
            else if (p == 0.0) { return "1.0"; }
            else
            {
                sb.Append("1.0");
                while ((p++) < 0)
                {
                    sb.AppendFormat(" / ({0})", value);
                }
            }
            return sb.ToString();
        }
    }
}
